package design

import (
	"github.com/goadesign/goa/design"
	"github.com/goadesign/goa/design/apidsl"
)

// UserName  string `json:"userName" valid:"Required"`
// Password  string `json:"password" valid:"Required"`
// CaptchaID string `json:"captchaID" valid:"Required"`
// Captcha   string `json:"captcha" valid:"Required"`

var _ = apidsl.Resource("auth", func() {
	apidsl.BasePath("/auth")
	apidsl.Response(design.InternalServerError, ErrMedia)
	apidsl.NoSecurity()
	apidsl.Response(design.BadRequest, ErrMedia)
	apidsl.Origin("*", func() {
		apidsl.Headers(" content-type") // One or more authorized headers, use "*" to authorize all
		apidsl.Methods("GET", "POST", "PUT", "DELETE")
		apidsl.MaxAge(600)
		apidsl.Credentials()
		apidsl.Expose("X-Time")
	})

	apidsl.Action("login", func() {
		apidsl.Description("登录")
		apidsl.Routing(apidsl.POST("/login_token"))

		apidsl.Payload(func() {
			apidsl.Member("userName", design.String, "用户名")
			apidsl.Member("password", design.String, "密码")
			apidsl.Member("captchaID", design.String, "验证码id")
			apidsl.Member("captcha", design.String, "验证码", func() {
				apidsl.MaxLength(4)
			})
			apidsl.Required("userName")
			apidsl.Required("password")
			apidsl.Required("captchaID")
			apidsl.Required("captcha")

		})
		apidsl.Response(design.OK, AuthResultMedia)
		apidsl.Response(design.Unauthorized, AuthErrorMedia)
	})
	apidsl.Action("refresh", func() {
		apidsl.Description("刷新令牌")
		apidsl.Routing(apidsl.POST("/refresh_token"))

		apidsl.Payload(func() {
			apidsl.Member("token", design.String, "当前令牌")
			apidsl.Required("token")
		})
		apidsl.Response(design.OK, AuthResultMedia)

		apidsl.Response(design.Unauthorized, AuthErrorMedia)
	})
	apidsl.Action("captchaid", func() {
		apidsl.Description("验证码")
		apidsl.Routing(apidsl.GET("/captchaid"))

		// apidsl.Payload(func() {
		// 	apidsl.Member("captchaid", design.String, "当前令牌")
		// 	apidsl.Required("captchaid")
		// })
		apidsl.Response(design.OK, CaptchaMedia)

	})
	apidsl.Action("captcha", func() {
		apidsl.Description("验证码")
		apidsl.Routing(apidsl.GET("/captcha/:id"))
		apidsl.Params(func() {
			apidsl.Param("reload")
		})
		apidsl.Response(design.OK, "image/png")

	})
})
